package com.nic.view.adapter;

import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.nic.view.R;
import com.nic.view.model.SpecialMultBean;
import com.nic.view.widget.MyGridView;

import java.util.Map;
import java.util.TreeMap;

/**
 * Created by Administrator on 2019/4/11.
 */

public class SpecialMultAdapter extends CommonAdapter<SpecialMultBean> {
    private Map<Integer, Map<String, String>> checkedList = new TreeMap<>();
    private SparseArray<Boolean> checkedArray = new SparseArray<>();

//    @Override
//    public void setList(List<SpecialMultBean> list) {
//        if (list != null && !list.isEmpty()) {
//            for (SpecialMultBean bean : list) {
//                checkedArray.put(bean.tag, false);
//            }
//        }
//        super.setList(list);
//    }

    @Override
    public View getBindView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pop_mult_special, null);
            holder.cbAll = (CheckBox) convertView.findViewById(R.id.cb_all);
            holder.tvDes = (TextView) convertView.findViewById(R.id.tv_desc);
            holder.gv = (MyGridView) convertView.findViewById(R.id.gv);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final SpecialMultBean bean = getList().get(position);
        holder.tvDes.setText("" + bean.subTitle);
        if (bean.itemList != null && !bean.itemList.isEmpty()) {
            final MultChooseAdapter adapter = new MultChooseAdapter();
            adapter.setList(bean.itemList);
            holder.cbAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (adapter.getList().isEmpty()) {
                        return;
                    }

                    if (isChecked) {
                        adapter.selectedAll();
                    } else {
                        adapter.unSelected();
                    }
                    checkedArray.put(bean.tag, isChecked);
                }
            });
            holder.cbAll.setChecked(checkedArray.get(bean.tag, false).booleanValue());
            adapter.setClickItemMultiListener(new CommonCheckAdapter.OnClickItemMultiListener() {
                @Override
                public void checkedItem(Map<String, String> map) {
                    int count = adapter.getCheckedMap().size();
                    int count1 = adapter.getList().size();
                    if (count == count1) {
                        checkedArray.put(bean.tag, true);
                    } else if (count == 0) {
                        checkedArray.put(bean.tag, false);
                    }
                    checkedList.put(bean.tag, map);
                    if (mOnClickItemListener != null)
                        mOnClickItemListener.checkedItem(checkedList);
                }
            });
            holder.gv.setAdapter(adapter);

        }

        return convertView;
    }


    class ViewHolder {
        CheckBox cbAll;
        TextView tvDes;
        MyGridView gv;
    }

    private OnClickItemListener mOnClickItemListener;

    public void setOnClickItemListener(OnClickItemListener mOnClickItemListener) {
        this.mOnClickItemListener = mOnClickItemListener;
    }

    public interface OnClickItemListener {
        void checkedItem(Map<Integer, Map<String, String>> checkedList);
    }
}
